
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: "",
    operaterPhone: "",
    isNeedBorrowMa: 2,
    payType: "",
    radioArr: [{
        img: "../../images/pay0.png",
        words: "余额",
        value: 0
      },
      {
        img: "../../images/pay1.png",
        words: "微信",
        value: 2
      }
    ],
    // 
    uid: "",
    userType: "",
    // 输入支付密码
    //是否展示密码输入层
    showPayPwdInput: false,
    //输入的密码
    payPasswords: "",
    //文本框焦点
    payFocus: true,
    // 
    isBusinessEnterIsDefaultPhone: "",
    businessEnterDefaultPhone: "",
    // 是否已经设置支付密码
    isAlreadyPayPassword: "",
    // 
    orderNumber:"",


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType'),
      isAlreadyPayPassword: wx.getStorageSync('isAlreadyPasswords'),
    })
    this.GetUserInfoByIDs();
    this.GetSJAccount();

  },

  //显示支付密码输入层
  showInputLayer: function() {
    this.setData({
      showPayPwdInput: true,
      payFocus: true
    });
  },
  //隐藏支付密码输入层
  cancelInputPasswords: function() {
    var val = this.data.payPasswords;
    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      payPasswords: ''
    });

  },
  // 获取焦点
  getFocus: function() {
    this.setData({
      payFocus: true
    });
  },
  //输入密码,监听
  inputPwd: function(e) {
    this.setData({
      payPasswords: e.detail.value
    });
  },
  //输入密码 确定 
  surePasswords: function() {
    var that = this;
    var uid = this.data.uid;
    var payPasswords = this.data.payPasswords;
    var orderNumber = this.data.orderNumber;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CheckMyTradePassword&UID=" + uid + "&TradePassword=" + payPasswords),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=BigCustomerPay&OrderNumber=" + orderNumber + "&PayType=0"),
            success: function(res) {
              var json = JSON.parse(baseJs.decryUrl(res.data))
              console.log(json);
              if (json.ResultFlag == "1") {
                wx.navigateBack({
                  delta: 1
                })
              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // GetUserInfoByIDs
  GetUserInfoByIDs: function() {
    var that = this;
    var uid = this.data.uid;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetUserInfoByIDs&UID=" + uid),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          that.setData({
            isBusinessEnterIsDefaultPhone: json.ResultData.IsBusinessEnterIsDefaultPhone,
            businessEnterDefaultPhone: json.ResultData.BusinessEnterDefaultPhone
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })

  },
  // GetSJAccount
  GetSJAccount: function() {
    var that = this;
    var uid = this.data.uid;
    var money = this.data.money;
    var operaterPhone = this.data.operaterPhone;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetSJAccount&SJID=" + uid),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          that.setData({
            money: parseFloat(json.ResultData.BigCustomerPayRmb).toFixed(2),
            operaterPhone: operaterPhone,
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // operaterPhone
  operaterPhone: function(e) {
    this.setData({
      operaterPhone: e.detail.value,
    })
  },
  // checkboxChange
  checkboxChange: function(e) {
    console.log(e);
    var that = this;
    var isNeedBorrowMa = this.data.isNeedBorrowMa;
    if (e.detail.value.length == 0) {
      isNeedBorrowMa = 2;
      this.setData({
        isNeedBorrowMa: isNeedBorrowMa,
      })
    } else {
      isNeedBorrowMa = 1;
      this.setData({
        isNeedBorrowMa: isNeedBorrowMa,
      })
    }
  },
  // radioChange余额，微信
  radioChange: function(e) {
    console.log(e);
    this.setData({
      payType: e.detail.value,
    })
  },
  // pay
  pay: function() {
    var that = this;
    var uid = this.data.uid;
    var money = this.data.money;
    var operaterPhone = this.data.operaterPhone;
    var isNeedBorrowMa = this.data.isNeedBorrowMa;
    var orderNumber = this.data.orderNumber;
    var payType = this.data.payType;
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    var isBusinessEnterIsDefaultPhone = this.data.isBusinessEnterIsDefaultPhone;
    var businessEnterDefaultPhone = this.data.businessEnterDefaultPhone;
    if (operaterPhone == "") {
      wx.showToast({
        title: '请输入运营中心手机号！',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (!myreg.test(operaterPhone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (payType == '') {
      wx.showToast({
        title: "请选择支付方式！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CreateBigCustomerOrder&UID=" + uid + "&ComeMaEdu=0" + "&PayRMB=" + money + "&Phone=" + operaterPhone + "&PlatFormType=weixin&IsNeedBorrowMa=" + isNeedBorrowMa + "&IsBusinessEnterIsDefaultPhone=" + isBusinessEnterIsDefaultPhone + "&BusinessEnterDefaultPhone=" + businessEnterDefaultPhone),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          orderNumber = json.ResultData;
          that.setData({
            orderNumber: orderNumber,
          })
          if (payType == "0") {
            // 余额
            if (isAlreadyPayPassword == '1') {
              that.setData({
                showPayPwdInput: true,
                payFocus: true
              })
            } else {
              wx.showToast({
                title: "请先去设置支付密码！",
                icon: 'none',
                duration: 2000
              });
            }

          } else {
            wx.request({
              url: baseJs.api + baseJs.encryUrl("method=BigCustomerPay&OrderNumber=" + orderNumber + "&PayType=2"),
              success: function(res) {
                var json = JSON.parse(baseJs.decryUrl(res.data));
                console.log(json);
                if (json.ResultFlag == 1) {
                  // wx.requestPayment({
                  //   timeStamp: json.ResultData.timestamp,
                  //   nonceStr: json.ResultData.nonce_str,
                  //   package: 'prepay_id=' + json.ResultData.prepay_id,
                  //   signType: 'MD5',
                  //   paySign: json.ResultData.sign,
                  //   success(res) {
                  //     console.log(res);
                  //     wx.navigateBack({
                  //       delta: 1
                  //     })
                  //   },
                  //   fail(res) {
                  //     console.log(res);
                  //     wx.showToast({
                  //       title: res.err_desc,
                  //       icon: 'none',
                  //       duration: 2000
                  //     });
                  //   }
                  // })

                } else {
                  wx.showToast({
                    title: json.ResultMsg,
                    icon: 'none',
                    duration: 2000
                  });
                }
              }
            })
          }
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }

      }
    })
  },
  // 
})